package cn.john.hrm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class HrmOAuth2WebSecurityConfig extends WebSecurityConfigurerAdapter {
    // 密码解析器
    @Bean
    public PasswordEncoder getEncoder(){
        return new BCryptPasswordEncoder();
    }

    // http安全配置

    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //.antMatchers("/login").permitAll()  // 登录接口放行
                .antMatchers("/loginUser/login").permitAll()    // 自己的接口
                .antMatchers("/loginUser/refresh").permitAll()  // 自己的接口
                .anyRequest().authenticated()                   // 其他请求都需要认证
                .and().formLogin()                              // 允许表单认证
                .successForwardUrl("/loginUser/success")
                .and().logout().permitAll()
                .and().csrf().disable();                        // 关闭跨站
    }
}
